package com.example.weather.city_manager;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.weather.MainActivity;
import com.example.weather.R;
import com.example.weather.data.City;
import com.example.weather.data.CityDao;
import com.example.weather.data.WeatherDatabase;

import java.util.ArrayList;
import java.util.List;


public class DeleteCityActivity extends AppCompatActivity implements View.OnClickListener{
    ImageView errorIv,rightIv;
    ListView deleteLv;
    String[]hotCitys = {"北京","太原","沈阳","重庆","天津","南宁","杭州","武汉","烟台","西安"};
    List<String> mDatas=new ArrayList<>();//列表数据源
    List<String>deleteCitys=new ArrayList<>();
    private WeatherDatabase weatherDatabase;
    private CityDao cityDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_delete_city);
        //找到组件
        deleteLv = findViewById(R.id.delete_lv);
        errorIv=findViewById(R.id.delete_iv_error);
        rightIv=findViewById(R.id.delete_iv_right);
        //获取数据
        weatherDatabase=WeatherDatabase.getDatabase(this);
        cityDao=weatherDatabase.getCityDao();
        mDatas=getData();

       //设置监听事件
        errorIv.setOnClickListener(this);
        rightIv.setOnClickListener(this);
        //设置适配器
        DeleteCityAdapter baseAdapter = new DeleteCityAdapter(this, mDatas, deleteCitys);
        deleteLv.setAdapter(baseAdapter);
        //设置点击事件的监听事件
        deleteLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int pos, long id) {
                TextView deletecity=view.findViewById(R.id.item_delete_tv);//找到点击的textview
                String cityname=deletecity.getText().toString().trim();//城市名
                mDatas.remove(cityname);
                deleteCitys.add(cityname);
                baseAdapter.notifyDataSetChanged();
            }
        });
    }
    private List<String>getData(){
        List cityList=new ArrayList<String>();
        List<City>cities=cityDao.findAll();
        for(City c:cities){
            cityList.add(c.getName());
        }
        return cityList;
    }
    public void cancel(View view){
        Intent intent_add=new Intent(this, CityManagerActivity.class);
        startActivity(intent_add);
    }
    public void ok(View view){
        for(String s:deleteCitys){
            cityDao.deleteByName(s);
        }
        finish();
    }
    @Override
    public void onClick(View view) {
    if (view.getId() == R.id.delete_iv_error) {
        cancel(view);
    } else if (view.getId() == R.id.delete_iv_right) {
        ok(view);
    }
}
}
